﻿@page "/archives"
@inherits BaseArchivePage

<PageContainer Title="Archives" PageTitle="@Title">
    <Breadcrumb>
        <Breadcrumb>
            <BreadcrumbItem>
                <a href="">
                    <MdiIcon Type="@IconNames.Home" />
                    <span>Home</span>
                </a>
            </BreadcrumbItem>
            <BreadcrumbItem>
                <span>
                    <MdiIcon Type="@IconNames.Archive" />
                    <span>Archives</span>
                </span>
            </BreadcrumbItem>
        </Breadcrumb>
    </Breadcrumb>
    <ChildContent>
        <Loader OnLoad="@OnLoad" LoadKey="@($"{PageNumber}")" @ref="loader">
            <Timeline Mode="alternate">
                @foreach (var item in Data)
                {
                    <TimelineItem Color="@($"#{((item.Key.GetHashCode() + 1) % 0xffffff + 1).ToString("X6")}")" Style="font-size: 16px;">
                        <p>@item.Key</p>
                        @foreach (var p in item.Value)
                        {
                            <p>
                                @switch (p.Type)
                                {
                                    case PostType.Article:
                                        <MdiIcon Type="@IconNames.Articles" />
                                        break;
                                    case PostType.Slides:
                                        <MdiIcon Type="@IconNames.Slides" />
                                        break;
                                    case PostType.Note:
                                        <MdiIcon Type="@IconNames.Notes" />
                                        break;
                                    default:
                                        break;
                                }
                                <a href="@UrlGenerator.Post(p)">@p.Title</a>
                            </p>
                        }
                    </TimelineItem>
                }
            </Timeline>
        </Loader>
    </ChildContent>
</PageContainer>

@code {
    const int PageSize = 10;

    Dictionary<string, List<Post>> Data { get; set; } = new Dictionary<string, List<Post>>();

    [Parameter]
    public int? PageNumber { get; set; } = null;

    private Loader loader;

    private async Task OnLoad()
    {
        Title = "Loading...";

        Data.Clear();
        var rawData = Service.PostService.GetAllItems();
        var grouped = (from d in rawData orderby d.CreationTime descending group d by d.CreationTime.ToLocalTime().ToString("yyyy-MM-dd"));
        await foreach (var item in grouped)
        {
            var list = new List<Post>();
            Data.Add(item.Key, list);
            await foreach (var p in item)
            {
                list.Add(p);
            }
        }
        Title = "";
    }
}
